package com.code.modules.base.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.code.modules.base.mapper.RelationMapper;
import com.code.modules.base.model.Relation;
import com.code.modules.base.service.RoleService;
import com.code.modules.base.mapper.RoleMapper;
import com.code.modules.base.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author MyBatisPlus
 * @since 2021-10-19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RelationMapper relationMapper;


    @Override
    public List<Role> findRoleByuserid(Integer userid) {
        if(userid == null) {userid = 0;}
        return roleMapper.getRoleByuserid(userid);
    }

    @Override
    public boolean checkRolecode(String code) {
        boolean flag = false;
        int num = roleMapper.checkRolecode(code).size();
        if(num == 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Role> getPageList(String condition) {
        return roleMapper.selectPageList(condition);
    }


    @Override
    public void setAuthority(Integer roldid, List<Integer> resids) {
        relationMapper.delRolesById(roldid);
        for (Integer id : resids) {
            Relation r = new Relation();
            r.setRoleid(roldid);
            r.setResid(id);
            relationMapper.insert(r);
        }
    }




}
